<?php

namespace App\Admin\Controllers;

use App\Models\CrawlerFilter;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Layout\Row;
use Encore\Admin\Layout\Column;

use App\Handlers\CrawlerUrlsHandler;
use App\Handlers\CrawlerArticleHandler;
use App\Models\Originurl;
use App\Models\CrawlerUrl;

class CrawlerFilterController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    public function test(CrawlerFilter $filter, Content $content)
    {
        // 抓取测试， 直接调用抓取器。
        $crawlerHandler = new CrawlerUrlsHandler($filter->origin_url);
        $list = $crawlerHandler->getList(true);
        if (! count($list['urls']) > 0) {
          $content->withWarning('抓取失败', '获取地址列表失败');
        }
        $article_url = $list['urls'][0]['url'];
        $urls = $list['urls'];
        $content->header('抓取测试');
        $content->description('抓取' . $filter->origin_url->title);

        $crawler_url = new CrawlerUrl;
        $crawler_url->url = $article_url;
        $crawler_url->origin_url_id = $filter->origin_url->id;
        $crawler_url->category_id = $filter->origin_url->category_id;
        $crawler_url->tags = $filter->origin_url->tags;

        $crawlerArticleHandler = new CrawlerArticleHandler($crawler_url);
        $article = $crawlerArticleHandler->getResult();

        $content->row(function (Row $row) use ($urls, $article) {
          $row->column(6, function (Column $column) use ($urls){
            $column->append($this->testListUrls($urls));
          });
          $row->column(6, function (Column $column) use ($article) {
            $column->append($this->showArticle($article));
          });
        });
        return $content;
    }
    protected function showArticle($article)
    {
      return view('admin.test.article', compact('article'));
    }
    protected function testListUrls($urls = null)
    {
        return view('admin.test.listurls', compact('urls'));
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CrawlerFilter);

        $grid->id('Id');
        $grid->domain('域名');
        $grid->list_filter('列表获取规则');
        $grid->link_filter('地址获取规则');
        $grid->title_filter('标题获取规则');
        $grid->content_filter('内容');
        $grid->images_filter('图片获取规则');

        $grid->origin_url()->title('标题');
        $grid->origin_url()->url('起始地址');
        $grid->status('测试状态');
        $grid->created_at('添加时间');
        $grid->updated_at('更新时间');
        $grid->actions(function ($actions) {
          $actions->append('<a href="' . route('crawler.filter.test', ['id' => $actions->row->id]) . '">测试</a>');
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CrawlerFilter::findOrFail($id));

        $show->id('Id');
        $show->domain('Domain');
        $show->list_filter('List filter');
        $show->link_filter('Link filter');
        $show->title_filter('Title filter');
        $show->content_filter('Conent filter');
        $show->images_filter('Images filter');
        $show->origin_url_id('Origin url id');
        $show->status('Status');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CrawlerFilter);

        $form->text('domain', '域名');
        $form->text('list_filter', '列表获取规则');
        $form->text('link_filter', '链接获取规则');
        $form->text('title_filter', '标题获取规则');
        $form->text('content_filter', '内容获取规则');
        $form->text('images_filter', '图片获取规则');
        $origins = Originurl::pluck('title', 'id');
        $form->select('origin_url_id', '起始地址')->options($origins);
        // $form->number('origin_url_id', '起始地址');
        $form->switch('status', '状态');

        return $form;
    }
}
